package com.gitee.somenvm.ninerpgitem.core.resolver.parser

import com.gitee.somenvm.ninerpgitem.core.resolver.Context
import com.gitee.somenvm.ninerpgitem.core.resolver.PlaceholderResolver
import taboolib.common.platform.function.warning
import taboolib.module.nms.ItemTagData
import taboolib.module.nms.ItemTagType
import taboolib.module.nms.getItemTag

/**
 * 用法 如
 * {nbt:某个插件绑定.owner}
 * */

object ParserNBT : PlaceholderResolver {
    override val id = "nbt"

    override fun resolve(source: String, placeholder: String, context: Context): List<String> {
        val data = context.item?.getItemTag()?.getDeep(placeholder) ?: return listOf("???<解析错误>").apply {
            warning("Inline - $context get NBT($placeholder) failed")
        }
        val value = source.replace(restore(placeholder), data.asString())

        return listOf(value)
    }


    private fun ItemTagData.asStringList(): List<String> {
        return when (type) {
            ItemTagType.LIST -> return asList().map { it.asString() }
            ItemTagType.BYTE_ARRAY -> asByteArray().map { it.toString() }
            ItemTagType.INT_ARRAY -> asIntArray().map { it.toString() }
            ItemTagType.LONG_ARRAY -> asLongArray().map { it.toString() }
            else -> listOf(asString())
        }
    }
}
